package com.bwie.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.*;
import com.bwie.service.*;
import com.bwie.mapper.SurplusMapper;
import com.bwie.utils.R;
import com.bwie.vo.SurplusVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author 61630
* @description 针对表【tb_surplus】的数据库操作Service实现
* @createDate 2024-08-06 14:27:07
*/
@Service
public class SurplusServiceImpl extends ServiceImpl<SurplusMapper, Surplus>
    implements SurplusService{
    @Resource
    private SurplusMapper surplusMapper;
    @Resource
    private ProvinceService provinceService;
    @Resource
    private BrandService brandService;

    @Resource
    private AppendService appendService;
    @Resource
    private StatusService statusService;
    @Resource
    private ImageService imageService;
    @Override
    public R getSurplusList(SurplusVo surplusVo) {
        QueryWrapper<Surplus> wrapper = new QueryWrapper<>();
        if(!surplusVo.getVehicleId().equals("")){
            wrapper.lambda().eq(Surplus::getVehicleId,surplusVo.getVehicleId());
        }
        if(Objects.nonNull(surplusVo.getStatusId())){
            wrapper.lambda().eq(Surplus::getStatusId,surplusVo.getStatusId());
        }
        if(Objects.nonNull(surplusVo.getAppendId())){
            wrapper.lambda().eq(Surplus::getAppendId,surplusVo.getAppendId());
        }
        if(Objects.nonNull(surplusVo.getProvinceId())){
            wrapper.lambda().eq(Surplus::getProvinceId,surplusVo.getProvinceId());
        }
        if(Objects.nonNull(surplusVo.getCityId())){
            wrapper.lambda().eq(Surplus::getCityId,surplusVo.getCityId());
        }
        if(Objects.nonNull(surplusVo.getCityId())){
            wrapper.lambda().eq(Surplus::getCityId,surplusVo.getCityId());
        }
        if(Objects.nonNull(surplusVo.getBrandId())){
            wrapper.lambda().eq(Surplus::getBrandId,surplusVo.getBrandId());
        }
        if(Objects.nonNull(surplusVo.getSeriesId())){
            wrapper.lambda().eq(Surplus::getSeriesId,surplusVo.getSeriesId());
        }
        if(Objects.nonNull(surplusVo.getModelId())){
            wrapper.lambda().eq(Surplus::getModelId,surplusVo.getModelId());
        }
        //        List<Surplus> surplusList = surplusMapper.selectList(wrapper);
        List<Province> provinceList = provinceService.list();
        List<Brand> brandList = brandService.list();
        List<Append> appendList = appendService.list();
        List<Status> statusList = statusService.list();
        List<Image> imageList = imageService.list();
//        __________________________________________________________________________________________________
        Page<Surplus> page = new Page<>(surplusVo.getPageNum(),surplusVo.getPageSize());
        Page<Surplus> pages = surplusMapper.selectPage(page, wrapper);
        List<Surplus> surplusList = pages.getRecords();
        for (Surplus surplus : surplusList) {
            for (Province province : provinceList) {
                if(surplus.getProvinceId()==province.getId()){
                    surplus.setProvinceName(province.getProvinceName());
                }
                if(surplus.getCityId()==province.getId()){
                    surplus.setCityName(province.getProvinceName());
                }
            }
            for (Brand brand : brandList) {
                if(surplus.getBrandId()==brand.getId()){
                    surplus.setBrandName(brand.getBrandName());
                }
                if(surplus.getSeriesId()==brand.getId()){
                    surplus.setSeriesName(brand.getBrandName());
                }
                if(surplus.getModelId()==brand.getId()) {
                    surplus.setModelName(brand.getBrandName());
                    surplus.setSurplusNum(brand.getBrandNumber());
                }
            }
            for (Append append : appendList) {
                if(surplus.getAppendId()==append.getId()){
                    surplus.setAppend(append);
                }
            }
            for (Status status : statusList) {
                if(surplus.getStatusId()==status.getId()){
                    surplus.setStatus(status);
                }
            }
            List<Image> images=new ArrayList<>();
            for (Image image : imageList) {
                if(surplus.getId()==image.getImgId()){
                    images.add(image);
                }
            }
            surplus.setImageList(images);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",surplusList);
        map.put("total",pages.getTotal());
        map.put("pages",pages.getPages());
        map.put("pageSize",pages.getSize());
        map.put("pageNum",pages.getCurrent());
        return R.success(map);
    }

    @Override
    public R insertSurplus(SurplusVo surplusVo) {
        Integer lastNum=surplusMapper.selectLastNum(surplusVo.getModelId());
        Integer num=0;
        for (Surplus surplus : surplusVo.getProvinceList()) {
            num+=surplus.getVehicleNum();
        }
        if(lastNum<=0||lastNum<=num){
            return R.error("数量不足");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("num",lastNum-num);
        map.put("modelId",surplusVo.getModelId());
        surplusMapper.subtractNum(map);
        for (Surplus surplus : surplusVo.getProvinceList()) {
            surplusVo.setProvinceId(surplus.getProvinceId());
            surplusVo.setCityId(surplus.getCityId());
            surplusVo.setVehicleNum(surplus.getVehicleNum());
            String s = RandomUtil.randomNumbers(9);
            surplusVo.setVehicleId("JCH"+s);
            Surplus surplus1 = BeanUtil.toBean(surplusVo, Surplus.class);
            surplusMapper.insert(surplus1);
            for (Image image : surplusVo.getImageList()) {
                image.setImgId(surplus1.getId());
                imageService.save(image);
            }
        }
        return R.success("添加成功");
    }

    @Override
    public R updateList() {
        QueryWrapper<Surplus> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Surplus::getStatusId,1);
        List<Surplus> surplusList = surplusMapper.selectList(wrapper);
        List<Province> provinceList = provinceService.list();
        List<Brand> brandList = brandService.list();
        List<Append> appendList = appendService.list();
        List<Status> statusList = statusService.list();
        List<Image> imageList = imageService.list();
        for (Surplus surplus : surplusList) {
            for (Province province : provinceList) {
                if(surplus.getProvinceId()==province.getId()){
                    surplus.setProvinceName(province.getProvinceName());
                }
                if(surplus.getCityId()==province.getId()){
                    surplus.setCityName(province.getProvinceName());
                }
            }
            for (Brand brand : brandList) {
                if(surplus.getBrandId()==brand.getId()){
                    surplus.setBrandName(brand.getBrandName());
                }
                if(surplus.getSeriesId()==brand.getId()){
                    surplus.setSeriesName(brand.getBrandName());
                }
                if(surplus.getModelId()==brand.getId()) {
                    surplus.setModelName(brand.getBrandName());
                    surplus.setSurplusNum(brand.getBrandNumber());
                }
            }
            for (Append append : appendList) {
                if(surplus.getAppendId()==append.getId()){
                    surplus.setAppend(append);
                }
            }
            for (Status status : statusList) {
                if(surplus.getStatusId()==status.getId()){
                    surplus.setStatus(status);
                }
            }
            List<Image> images=new ArrayList<>();
            for (Image image : imageList) {
                if(surplus.getId()==image.getImgId()){
                    images.add(image);
                }
            }
            surplus.setImageList(images);
        }
        return R.success(surplusList);


    }

    @Override
    public R findSurplusById(Integer id) {
        Surplus surplus = surplusMapper.selectById(id);
        List<Image> list = imageService.list();
        List<Image> images = new ArrayList<>();
        for (Image image : list) {
            if(image.getImgId()==id){
                images.add(image);
            }
        }
        surplus.setImageList(images);
        return R.success(surplus);
    }

    @Override
    public R sureUpdate(SurplusVo surplusVo) {
        Surplus surplus = BeanUtil.toBean(surplusVo, Surplus.class);
        surplusMapper.updateById(surplus);
        imageService.removeByImgId(surplus.getId());
        imageService.saveBatch(surplus.getImageList());
        return R.success("修改成功");
    }
}




